import type { FormItemRule} from 'naive-ui'



// 返回一些常用的表单验证规则
export const useRules = ():{
  required: (msg:string) => FormItemRule[]
  requiredForNumber: (msg:string) => FormItemRule[]
  requiredForArray: (msg:string) => FormItemRule[]
  requiredForObject: (msg:string) => FormItemRule[]
  username: (msg:string) => FormItemRule[]
  password: (msg:string) => FormItemRule[]
  idCard: (msg:string) => FormItemRule[]
  phone: (msg:string) => FormItemRule[]
  email: (msg:string) => FormItemRule[]
  age: (msg:string) => FormItemRule[]
  [key:string]:(msg:string) => FormItemRule[]
} => {
  return {
    // 必填
    required:(msg:string) => [{ required: true, message: msg || '必填项不能为空', trigger: 'blur' }],
    requiredForNumber: (msg:string) => [{ required: true, message: msg || '必填项不能为空', trigger: 'blur',validator: (_, value) => value !== undefined && value !== null}],
    requiredForArray: (msg:string) => [{ required: true, message: msg || '必填项不能为空', trigger: 'change',validator: (_, value) => value.length > 0, }],
    requiredForObject: (msg:string) => [{ required: true, message: msg || '必填项不能为空', trigger: 'change',validator: (_, value) => Object.keys(value).length > 0, }],
    // 用户名，八位以上，字母数字下划线
    username: (msg:string) => [
      {
        validator: validateUsername,
        message: msg || '用户名必须大于8位,且只能包含字母数字下划线',
        trigger: 'blur',
      },
    ],
    // 密码，八位以上，字母数字下划线
    password: (msg:string) => [
      {
        validator: validatePassword,
        message: msg || '密码必须大于8位,且只能包含字母数字下划线',
        trigger: 'blur',
      },
    ],
    // 身份证号
    idCard: (msg:string) => [
      {
        validator: validateIdCard,
        message: msg || '请输入正确的身份证号',
        trigger: 'blur',
      },
    ],
    // 手机号
    phone: (msg:string) => [
      {
        validator: validatePhone,
        message: msg || '请输入正确的手机号',
        trigger: 'blur',
      },
    ],
    // 邮箱
    email: (msg:string) => [
      {
        validator: validateEmail,
        message: msg || '请输入正确的邮箱',
        trigger: 'blur',
      },
    ],
    // 年龄
    age: (msg:string) => [
      {
        validator: validateAge,
        message: msg || '请输入正确的年龄',
        trigger: 'blur',
      },
    ],
  }
}

// 验证用户名
const validateUsername = (rule: FormItemRule, value: string): boolean => {
  const reg = /^[a-zA-Z0-9_]{8,}$/
  return reg.test(value);

}

// 验证密码
const validatePassword = (rule: FormItemRule, value: string): boolean => {
  const reg = /^[a-zA-Z0-9_]{8,}$/
  return reg.test(value);

}

// 身份证号
const validateIdCard = (rule: FormItemRule, value: string): boolean => {
  const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  return reg.test(value);

}

// 手机号
const validatePhone = (rule: FormItemRule, value: string): boolean => {
  const reg = /^1[3456789]\d{9}$/
  return reg.test(value);

}

// 邮箱
const validateEmail = (rule: FormItemRule, value: string): boolean => {
  const reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+$/
  return reg.test(value);

}

// 年龄验证，必须整数
const validateAge = (rule: FormItemRule, value: string): boolean => {
  const reg = /^[0-9]*$/
  return reg.test(value);
}
